<?php
class CategoryModel
{
	public static function GetCategory()
	{
		include_once 'sql_connector.php';
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");

		$query = "select id, name, parentid, level from categories";
		try {
			$result = $db->fetchAll($query);
		}
		catch(Zend_Db_Exception $e)
		{
			echo 'DB ERROR : '.$e->getMessage();
		}
		
		return $result;
	}
	
	public static function GetMaxLevel()
	{
		include_once 'sql_connector.php';
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		
		$query = "select max(Level) from Categories";
		try {
			$result = $db->fetchOne($query);
		}
		catch(Zend_Db_Exception $e)
		{
			echo 'DB ERROR : '.$e->getMessage();
		}
		
		return $result;
	}
	
	public static function GetIDFromName($name)
	{
		include_once 'sql_connector.php';
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		
		$query = "select id from Categories where name = '$name'";
		try {
			$result = $db->fetchOne($query);
		}
		catch(Zend_Db_Exception $e)
		{
			echo 'DB ERROR : '.$e->getMessage();
		}
		
		return $result;
	}
	
	public static function GetNameFromID($id)
	{
		include_once 'sql_connector.php';
		$db = SQLConnector::connect();
		$db->query("SET NAMES 'utf8'");
		
		$query = "select name from Categories where id = '$id'";
		try {
			$result = $db->fetchOne($query);
		}
		catch(Zend_Db_Exception $e)
		{
			echo 'DB ERROR : '.$e->getMessage();
		}
		
		return $result;
	}
}